/******************************************************文本框*****************************************************/
_$.CheckBox=function (target){
    var _this = this;
    _$.CheckBox.superclass.constructor.call(_this,target);
};
_$.regClass("CheckBox",_$.CheckBox);

_$.extend(_$.CheckBox,_$.FormItem, {
    _cls:_$._clsPre + "CheckBox",
    _uiCls:_$._uiPreCls + "CheckBox",
    _attrProps:function(){
        var _this = this;
        var properties = _$.CheckBox.superclass._attrProps.call(_this);
        properties.addAll(["onchange","idField","textField",{data:'object'}]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.CheckBox.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            "idField":"id",
            "textField":"name"
        });
    },
    _editModel:function(){
        var _this = this;
        _this._clearShow();

        var source='<div class="pretty checkBox"></div>';
        _this._replaceNode(source);
        _this.component = $(".checkBox",_this.el);
        _this.el.addClass(_this._uiCls);
        _this._initCheckBox();
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._init();
        _this._bindEvents();
    },
    _initCheckBox:function(){
        var _this = this;
        var data = _this.getOption("data");
        _this.data = data;
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        var value = data[idField];
        var name = data[textField];
        var _html = '<input type="checkbox" value="'+value+'" /><label><i class="mdi mdi-check"></i>&nbsp;'+name+'</label>';
        _this.component.html(_html);
        _this.checkbox_input = $("input",_this.component);
        _this._bindCheckBoxEvent();
        _this._loadSuccess();
    },
    _init:function(){
        var _this = this;
        if(!_this._isEdit()){
            var data = _this.getOption("data");
            _this.data = data;
            var initValue= _this.options["initValue"];
            _this.setValue(initValue);
        }
        _this._loadSuccess();
    },
    _bindCheckBoxEvent:function(){
        var _this = this;
        _this.checkbox_input.bind("click",function(e){
            event['source'] = _this.component;
            event['value'] = _this.getValue();
            _this._clickHandler(event);
        });
    },
    setValue:function(value,change){
        var _this = this;
        if(isEmpty(change)){
            change = true;
        }
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        if(isEmpty(value) || isEmpty(_this.data)){
            _this.value = "";
            if(_this._isEdit()){
                _this.checkbox_input.prop("checked",false);
            }else{
                _this.component.html("")
            }
        }else{
            var orgi = _this.data[idField];
            var name = _this.data[textField];
          if(_this._isEdit()){
              if(orgi == value){
                  _this.value = value;
                  _this.checkbox_input.prop("checked",true);
              }else{
                  _this.value = null;
                  _this.checkbox_input.prop("checked",false);
              }
          }else{
              if(orgi == value){
                  _this.value = value;
                  _this.component.html(name)
              }else{
                  _this.value = null;
                  _this.component.html("")
              }
          }
        }
        if(change){
            _this._fire("onchange");
        }
    },
    _clickHandler:function(event){
        var _this = this;
        _this._fire("onclick",event);
        var original = isNotEmpty(_this.original)?_this.original:_this.getOption("initValue");
        var value = _this.getValue();
        if(original != value){
            _this.original=value;
            _this._fire("onchange",event);
        }
    },
    _sync:function(){
        var _this = this;
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        if(_this.checkbox_input.is(":checked")){
           _this.value =  _this.data[idField];
        }else{
            _this.value = null;
        }
    }
});
